//
//  EmotionKeyBordView.swift
//  新浪微博项目
//
//  Created by 王炯 on 16/7/29.
//  Copyright © 2016年 王炯. All rights reserved.
//

import UIKit

class EmotionKeyBordView: UIView
{

    //懒加载显示底部3个button的View
    private lazy var buttonsView:EmotionKeyBordButtons = EmotionKeyBordButtons(frame: CGRectZero)
    
    //懒加载collectionView
    private lazy var emotionView:EmotionCollectionView = {
       
        
        let collecTionView = EmotionCollectionView()
        
        
        //在此设置collecTionView
        //(1)分页效果
        collecTionView.pagingEnabled = true
        //(2)弹簧效果
        collecTionView.bounces = false
        //(3)隐藏滚动条
        collecTionView.showsVerticalScrollIndicator = false
        collecTionView.showsHorizontalScrollIndicator = false
        
        //(4)设置colleTionView的代理方法
        collecTionView.delegate = self
        
        return collecTionView
        
    }()
    
    
    //懒加载一个pageController
    private lazy var pageContoller:UIPageControl = {
       
        let page = UIPageControl()
        
        
         // 在xcode 6.4 可以看到 以后苹果直接就给他隐藏了 但是你知道名字 可以通过kvc赋值!!!!!!
        page.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKey: "pageImage")
        
         page.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKey: "currentPageImage")
        
        
        return page
        
    }()
    
        
    override init(frame: CGRect)
    {
        super.init(frame: frame)
        
        //布局界面
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //布局界面
    func setUpUI()
    {
       backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        
        //添加底部的buttonView
        addSubview(self.buttonsView)
        
        //添加表情collectionView
        addSubview(self.emotionView)
        
        //添加pageController
        addSubview(self.pageContoller)
        
        
        
        
        //设置约束
        self.buttonsView.snp_makeConstraints { (make) in
            make.leading.bottom.trailing.equalTo(self)
            make.height.equalTo(37)
        }
        
        
        
        //设置buttonView的点击触发事件
        self.buttonsView.buttonClosure = {[weak self] (type:ButtonType) -> () in
            
            switch type
            {
            case .defaultType:
                print("默认")
            case .emojiType:
                print("emoji")
            case .xlhType:
                print("浪小花")
            }
            
            //(1)根据button的tag值来获取indexPath
            let indexPath:NSIndexPath = NSIndexPath(forItem: 0, inSection: type.rawValue)
            
            //跳转到响应的组内
            self?.emotionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.None, animated: false)
            
            //点击button之后设置pageController
            self?.setUpPageController(indexPath)
            
        }
        
        
        //设置collectionView的约束
        self.emotionView.snp_makeConstraints { (make) in
            
            make.leading.top.trailing.equalTo(self)
            make.bottom.equalTo(self.buttonsView.snp_top)
        }
        
        //设置pageControll约束
        self.pageContoller.snp_makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.bottom.equalTo(self.emotionView).offset(8)
        }
        
        
        let indexPath:NSIndexPath = NSIndexPath(forItem: 0, inSection: 0)
        
        //初始化pageController
        self.setUpPageController(indexPath)
        
    }
}
//在此扩展内实现collecTionView的代理方法
extension EmotionKeyBordView:UICollectionViewDelegate
{
    func scrollViewDidScroll(scrollView: UIScrollView)
    {
          //(1)获取相对于与collectionView 中心点X
        let centerX = scrollView.contentOffset.x + SCREENW * 0.5
        
        
        //(2)拿到屏幕显示的cell数组!!!!!!!!(因为cell的宽度==屏幕的宽度，所以屏幕显示的cell数<=2）
        //(3)并对获取到的视图cells数组里的cell数据按照x轴的大小从小到大排序!!!!!
        let cells = self.emotionView.visibleCells().sort { (cell1, cell2) -> Bool in
            
            //按照数组中的元素从小到大的排序
            return cell1.frame.origin.x < cell2.frame.origin.x
        }
        
        //(4)如果屏幕中有两个cell，我们才做判断
        if cells.count == 2
        {
            let firstCell = cells.first!
            let lastCell = cells.last!
        
            //定义indexPath
            var indexPath:NSIndexPath
            
            //(5)判断当前屏幕上显示的cell是哪一个cell包含了中心点
            if firstCell.frame.contains(CGPoint(x: centerX, y: 0))
            {
                indexPath = self.emotionView.indexPathForCell(firstCell)!
            }
            else
            {
                indexPath = self.emotionView.indexPathForCell(lastCell)!
            }
            
            
            //print("显示第\(indexPath.section)组")
            
            //(6)传递indexPath.section，改变button的状态
            self.buttonsView.changeButtonStatusWithSection(indexPath.section)
            
            //(7)根据indexPath来设置pageController
            self.setUpPageController(indexPath)
            
        }
         
    }
    
    
    //设置pageController的总页数和当前页数
    func setUpPageController(indexPath:NSIndexPath)
    {
        //总页数
        self.pageContoller.numberOfPages = EmotionDataTool.sharedTool.AllEmotions[indexPath.section].count
        
        //当前页
        self.pageContoller.currentPage = indexPath.item
    
    }

}
    
